const express = require("express");
const cors = require("cors");
const app = express();
var path = require("path"); //系统路径模块
var fs = require("fs"); //文件模块

var server = app.listen(9090, "111.231.189.253", function() {
    console.log("服务启动成功！");
});

/********************socketIO********************/
var io = require("socket.io").listen(server);
// 建立连接
io.sockets.on("connection", function(socket) {
    console.log("已链接");

    // 连接断开，如关闭页面时触发
    socket.on("disconnect", function() {
        console.log("已断开链接");
    });
    //发送给客户端的消息（每隔5秒）
    setInterval(() => {
      createDate();
    }, 10000);

    function createDate() {
        let myDate = new Date();
        let list1 = [];
        let list2 = [];
        let chartList1 = [];
        let chartList2 = [];
        let chartList3 = [];
        let chartList4 = [];
        for (i = 1; i <= 10; i++) {
            list1.push([
                new Date(
                    myDate.setDate(myDate.getDate() + i)
                ).toLocaleDateString(),
                "中久物流",
                "CF" + Math.floor(Math.random() * Math.floor(100)),
                Math.floor(Math.random() * Math.floor(100)),
                "张三"
            ]);
            list2.push([
                new Date(
                    myDate.setDate(myDate.getDate() + i)
                ).toLocaleDateString(),
                "中久物流",
                "沈阳物流",
                "CF" + Math.floor(Math.random() * Math.floor(100)),
                Math.floor(Math.random() * Math.floor(100)),
                "张三"
            ]);
        }
        let dateList = [];
        let valueList = [];
        myDate = new Date();
        dateList.push(
                    formatTime(new Date(
                        myDate.setDate(new Date().getDate() - 7)
                    )),
                    formatTime(new Date(
                        myDate.setDate(new Date().getDate() - 6)
                    )),
                    formatTime(new Date(
                        myDate.setDate(new Date().getDate() - 5)
                    )),
                    formatTime(new Date(
                        myDate.setDate(new Date().getDate() - 4)
                    )),
                    formatTime(new Date(
                        myDate.setDate(new Date().getDate() - 3)
                    )),
                    formatTime(new Date(
                        myDate.setDate(new Date().getDate() - 2)
                    )),
                    formatTime(new Date(
                        myDate.setDate(new Date().getDate() - 1)
                    ))
                )
                valueList.push(
                    Math.floor(Math.random() * Math.floor(100)),
                    Math.floor(Math.random() * Math.floor(200)),
                    Math.floor(Math.random() * Math.floor(300)),
                    Math.floor(Math.random() * Math.floor(400)),
                    Math.floor(Math.random() * Math.floor(500)),
                    Math.floor(Math.random() * Math.floor(100)),
                    Math.floor(Math.random() * Math.floor(100))
                )

        let chart1 = {
            title: "大庆CMC",
            data: [
                {
                    name: "CFM19117-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21411T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG214117-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21210T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG212101-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21179T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                }
            ]
        };
        let chart2 = {
            title: "长春CMC",
            data: [
                {
                    name: "CFM19117-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21411T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG214117-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21210T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG212101-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21179T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                }
            ]
        };
        let chart3 = {
            title: "成都CMC",
            data: [
                {
                    name: "CFM19117-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21411T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG214117-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21210T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG212101-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21179T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                }
            ]
        };
        let chart4 = {
            title: "路桥CMC",
            data: [
                {
                    name: "CFM19117-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21411T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG214117-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21210T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG212101-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21179T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                }
            ]
        };
        let chart5 = {
            title: "苏州CMC",
            data: [
                {
                    name: "CFM19117-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21411T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG214117-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21210T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG212101-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21179T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                }
            ]
        };
        let chart6 = {
            title: "沈阳CMC",
            data: [
                {
                    name: "CFM19117-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21411T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG214117-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21210T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG212101-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                },
                {
                    name: "CFG21179T-PL",
                    value: Math.floor(Math.random() * Math.floor(100))
                }
            ]
        };

        chartList1.push(chart1);
        chartList1.push(chart2);
        chartList1.push(chart3);
        chartList1.push(chart4);
        chartList1.push(chart5);
        chartList1.push(chart6);

        chartList2.push(chart1);
        chartList2.push(chart2);
        chartList2.push(chart3);
        chartList2.push(chart4);
        chartList2.push(chart5);
        chartList2.push(chart6);

        chartList3.push(chart1);
        chartList3.push(chart2);
        chartList3.push(chart3);
        chartList3.push(chart4);
        chartList3.push(chart5);
        chartList3.push(chart6);

        let symbolList = [
            [
              '北京',
              [
                [{ name: '北京' }, { name: '上海', value: 95 }],
                [{ name: '北京' }, { name: '广州', value: 90 }],
                [{ name: '北京' }, { name: '大连', value: 80 }],
                [{ name: '北京' }, { name: '南宁', value: 70 }],
                [{ name: '北京' }, { name: '南昌', value: 60 }],
                [{ name: '北京' }, { name: '拉萨', value: 50 }],
                [{ name: '北京' }, { name: '长春', value: 40 }],
                [{ name: '北京' }, { name: '包头', value: 30 }],
                [{ name: '北京' }, { name: '重庆', value: 20 }],
                [{ name: '北京' }, { name: '常州', value: 10 }]
              ]
            ],
            [
              '上海',
              [
                [{ name: '上海' }, { name: '包头', value: 95 }],
                [{ name: '上海' }, { name: '昆明', value: 90 }],
                [{ name: '上海' }, { name: '广州', value: 80 }],
                [{ name: '上海' }, { name: '郑州', value: 70 }],
                [{ name: '上海' }, { name: '长春', value: 60 }],
                [{ name: '上海' }, { name: '重庆', value: 50 }],
                [{ name: '上海' }, { name: '长沙', value: 40 }],
                [{ name: '上海' }, { name: '北京', value: 30 }],
                [{ name: '上海' }, { name: '丹东', value: 20 }],
                [{ name: '上海' }, { name: '大连', value: 10 }]
              ]
            ],
            [
              '广州',
              [
                [
                  { name: '广州', value: 100 },
                  { name: '福州', value: 95 }
                ],
                [{ name: '广州' }, { name: '太原', value: 90 }],
                [{ name: '广州' }, { name: '长春', value: 80 }],
                [{ name: '广州' }, { name: '重庆', value: 70 }],
                [{ name: '广州' }, { name: '西安', value: 60 }],
                [{ name: '广州' }, { name: '成都', value: 50 }],
                [{ name: '广州' }, { name: '常州', value: 40 }],
                [{ name: '广州' }, { name: '北京', value: 30 }],
                [{ name: '广州' }, { name: '北海', value: 20 }],
                [{ name: '广州' }, { name: '海口', value: 10 }]
              ]
            ]
          ]
        let symbolData = ['北京', '上海', '广州']
        let colors = ['#a6c84c', '#ffa022', '#46bee9']


        let jsonStr = JSON.stringify({
            numset: {
                containerTotal: Math.floor(Math.random() * Math.floor(100000)),
                containerKind: Math.floor(Math.random() * Math.floor(100000)),
                containerCount: Math.floor(Math.random() * Math.floor(100000)),
                inStorageCount: Math.floor(Math.random() * Math.floor(100000)),
                sendedCount: Math.floor(Math.random() * Math.floor(100000)),
                unSendCount: Math.floor(Math.random() * Math.floor(100000)),
                reviveCount: Math.floor(Math.random() * Math.floor(100000)),
                inStorageCount: Math.floor(Math.random() * Math.floor(100000)),
                transferListCount: Math.floor(
                    Math.random() * Math.floor(100000)
                )
            },
            chart1: chartList1,
            chart2: chartList2,
            chart3: chartList3,
            chart4: {'date':dateList,'value':valueList},
            chart5: {'symbolList':symbolList,'symbolData':symbolData,'colors':colors},
            list1: list1,
            list2: list2
        });
        console.table("send text :" + jsonStr);
        socket.emit("pvmanage", jsonStr);
    }

    function formatTime(date) {
        var month = date.getMonth() + 1, month = month < 10 ? '0' + month : month;
        var day = date.getDate(), day = day < 10 ? '0' + day : day;
        return month + '/' + day;
    }
    createDate();
});
